package com.zhida.smartcontrol.pagectrl.editproject;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jay.tools.base.BaseDialogFragment;
import com.jay.tools.ui.TopBarView;
import com.jay.tools.utils.EventBusUtils;
import com.jay.tools.utils.SoftInputUtils;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.mapsel.MapSelFragment;
import com.zhida.smartcontrol.mvp.MVPBaseFragment;
import com.zhida.smartcontrol.dialog.OkDialogFragment;
import com.zhida.smartcontrol.pagectrl.ProjectListBean;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/10/9 009 16:27
 * description: 编辑项目
 */
public class EditProjectFragment extends MVPBaseFragment<EditProjectContract.View, EditProjectPresenter> implements EditProjectContract.View {
    @BindView(R.id.top)
    TopBarView mTopBar;
    @BindView(R.id.et_project_name)
    EditText mEtPName;
    @BindView(R.id.et_project_address)
    EditText mEtAddress;
    @BindView(R.id.et_project_manager)
    EditText mEtManager;
    @BindView(R.id.et_project_phone)
    EditText mEtPhone;
    @BindView(R.id.tv_jd)
    TextView mTvJd;
    @BindView(R.id.tv_wd)
    TextView mTvWd;
    @BindView(R.id.btn_add)
    Button mBtnAdd;

    private String mJd;
    private String mWd;

    private String mId;
    private int mType;

    public static EditProjectFragment newInstance(String id, int type) {
        Bundle args = new Bundle();
        args.putString("id", id);
        args.putInt("type", type);
        EditProjectFragment fragment = new EditProjectFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_project_edit;
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        stateBar(true);
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        stateBar(false);
        SoftInputUtils.hideSoftInput(getProxyActivity());
    }

    @Override
    protected void initLazy(Bundle savedInstanceState) {
        inflateTopBar(mTopBar);

        Bundle bundle = getArguments();
        if (null == bundle) {
            return;
        }
        mType = bundle.getInt("type");
        if (mType == Const.TYPE_EDIT) {
            mId = bundle.getString("id");
            mPresenter.requestInfo(mId);
            mBtnAdd.setText("保存修改");
        } else {
            mBtnAdd.setText("添加");
        }
    }

    @OnClick({R.id.rl_jwd, R.id.btn_add})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.rl_jwd:
                SoftInputUtils.hideSoftInput(getProxyActivity());
                requestLocation();
                break;
            case R.id.btn_add:
                String pName = mEtPName.getText().toString().trim();
                String address = mEtAddress.getText().toString().trim();
                String manager = mEtManager.getText().toString().trim();
                String phone = mEtPhone.getText().toString().trim();

                mPresenter.submit(mType, mId, pName, manager, address, phone, mWd, mJd);
                break;
        }
    }

    @Override
    protected void location(boolean hasLocation) {
        startForResult(MapSelFragment.newInstance(), 0);
    }

    @Override
    public void showInfo(EditProjectBean bean) {
        mEtPName.setText(bean.getProjectName());
        mEtAddress.setText(bean.getAddress());
        mEtPhone.setText(bean.getManagerMobile());
        mEtManager.setText(bean.getManagerName());
        mWd = bean.getLat();
        mJd = bean.getLng();
        mTvWd.setText(bean.getLat());
        mTvJd.setText(bean.getLng());
    }

    @Override
    public void onSuccess(String msg) {
        OkDialogFragment instance = OkDialogFragment.newInstance(msg);
        instance.showDialog(getChildFragmentManager());
        instance.setOnDestroyListener(new BaseDialogFragment.OnDestroyListener() {
            @Override
            public void onDestroy(Object object, boolean isChange) {
                EventBusUtils.post(new ProjectListBean());
                pop();
            }
        });
    }

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        super.onFragmentResult(requestCode, resultCode, data);
        if (resultCode != 0) {
            return;
        }
        if (null == data) {
            return;
        }
        mWd = data.getString("lat");
        mJd = data.getString("lng");
        mTvJd.setText(mJd);
        mTvWd.setText(mWd);
    }


}
